//
//  EnhanceBar.m
//  GPUImage_MyDemo
//
//  Created by yuedongkui on 2018/5/21.
//  Copyright © 2018年 LY. All rights reserved.
//

#import "EnhanceBarView.h"
#import "BottomNavBar.h"

static NSString *kEnhanceCellReuseIdentifier = @"kEnhanceCellReuseIdentifier";


@interface EnhanceBarView ()<UICollectionViewDelegate, UICollectionViewDataSource, BottomNavBarDelegate>

@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, strong) NSMutableArray <EnhanceItem *>*lists;
@property (nonatomic, strong) BottomNavBar *navBar;

@property (nonatomic, strong) UISlider *slider;

@end



@implementation EnhanceBarView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        self.backgroundColor = [UIColor blueColor];
        
//        NSArray *titles = @[@"亮度", @"饱和度", @"对比度", @"色温", @"模糊", @"高光", @"阴影", @"暗角", @"锐化", @"色阶"];
        NSArray *titles = @[@"亮度", @"曝光度", @"对比度", @"饱和度", @"伽马值", @"色阶", @"色相", @"白平衡", @"阴影", @"高光"];

        self.lists = [NSMutableArray array];
        [titles enumerateObjectsUsingBlock:^(NSString *title, NSUInteger idx, BOOL * _Nonnull stop) {
            EnhanceItem *item = [[EnhanceItem alloc] init];
            item.title = title;
            item.isSelected = NO;
            if (idx == 0) {
                item.isSelected = YES;
            }
            [self.lists addObject:item];
        }];
        
        
        self.slider = [[UISlider alloc] initWithFrame:CGRectZero];
        [self.slider addTarget:self action:@selector(sliderValueChanged) forControlEvents:UIControlEventTouchUpInside];
//        [self.slider addTarget:self action:@selector(sliderValueChanged) forControlEvents:UIControlEventValueChanged];

        [self addSubview:self.slider];
        [self.slider mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self).offset(20);
            make.right.equalTo(self).offset(-20);
            make.top.equalTo(self).offset(0);
            make.height.mas_equalTo(30);
        }];
        
        UICollectionViewFlowLayout *collectionViewLayout = [[UICollectionViewFlowLayout alloc] init];
        [collectionViewLayout setItemSize:CGSizeMake(kEnhanceBarCellWidth, kEnhanceBarCellHeight)];
        [collectionViewLayout setScrollDirection:UICollectionViewScrollDirectionHorizontal];
        collectionViewLayout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
        collectionViewLayout.minimumInteritemSpacing = 0;
        collectionViewLayout.minimumLineSpacing = 0;
        
        self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:collectionViewLayout];
        self.collectionView.backgroundColor = [UIColor whiteColor];
        [self.collectionView registerClass:[EnhanceCell class] forCellWithReuseIdentifier:kEnhanceCellReuseIdentifier];
        self.collectionView.delegate = self;
        self.collectionView.dataSource = self;
        [self addSubview:self.collectionView];
        [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.equalTo(self).offset(0);
            make.top.equalTo(self).offset(30);
            make.height.mas_equalTo(kEnhanceBarCellHeight);
        }];
        
        self.navBar = [[BottomNavBar alloc] initWithFrame:CGRectZero];
        self.navBar.delegate = self;
        [self addSubview:self.navBar];
        [self.navBar mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.equalTo(self).offset(0);
            make.top.equalTo(self.collectionView.mas_bottom).offset(0);
            make.bottom.equalTo(self);
        }];
    }
    return self;
}

- (void)configMinValue:(CGFloat)min maxValue:(CGFloat)maxValue defaultValue:(CGFloat)defaultValue;
{
    self.slider.minimumValue = min;
    self.slider.maximumValue = maxValue;
    self.slider.value = defaultValue;
}

#pragma mark -
- (void)sliderValueChanged
{
    [self.delegate enhanceBarView:self didChangeValue:self.slider.value];
}

#pragma mark - UICollectionViewDelegate & UICollectionViewDataSource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.lists.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    EnhanceCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:kEnhanceCellReuseIdentifier forIndexPath:indexPath];
    cell.backgroundColor = [UIColor lightGrayColor];
    EnhanceItem *item = self.lists[indexPath.row];
    cell.titleLabel.text = item.title;
    if (item.isSelected) {
        cell.iconImageView.image = [UIImage imageNamed:@"abc_ic_star_black_48dp_h"];
    } else {
        cell.iconImageView.image = [UIImage imageNamed:@"abc_ic_star_black_48dp"];
    }
    
    return cell;
}

//- (void)collectionView:(UICollectionView *)collectionView didHighlightItemAtIndexPath:(nonnull NSIndexPath *)indexPath
//{
//    EnhanceCell *cell = (EnhanceCell *)[collectionView cellForItemAtIndexPath:indexPath];
//    cell.iconImageView.image = [UIImage imageNamed:@"abc_ic_star_black_48dp_h"];
//}
//
//- (void)collectionView:(UICollectionView *)collectionView didUnhighlightItemAtIndexPath:(nonnull NSIndexPath *)indexPath
//{
//    EnhanceCell *cell = (EnhanceCell *)[collectionView cellForItemAtIndexPath:indexPath];
//    cell.iconImageView.image = [UIImage imageNamed:@"abc_ic_star_black_48dp"];
//}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    [self.delegate enhanceBarView:self didSelectRow:indexPath.row];

    [self.lists enumerateObjectsUsingBlock:^(EnhanceItem * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        obj.isSelected = NO;
    }];
    EnhanceItem *item = self.lists[indexPath.row];
    item.isSelected = YES;
    [collectionView reloadData];
}

#pragma mark - BottomNavBarDelegate
- (void)bottomNavBarDidBack:(BottomNavBar *)bar
{
    [self.delegate enhanceBarDidBack:self];
}

- (void)bottomNavBarDidConfirm:(BottomNavBar *)bar
{
    [self.delegate enhanceBarDidConfirm:self];
}


@end





@implementation EnhanceCell

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        self.iconImageView = [[UIImageView alloc] initWithFrame:CGRectZero];
        self.iconImageView.image = [UIImage imageNamed:@"abc_ic_star_black_48dp"];
        [self addSubview:self.iconImageView];
        [self.iconImageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.top.equalTo(self).offset(0);
            make.size.mas_offset(CGSizeMake(kEnhanceBarCellWidth, kEnhanceBarCellHeight-20));
        }];
        
        self.titleLabel = [[UILabel alloc] initWithFrame:CGRectZero];
        self.titleLabel.textColor = [UIColor blackColor];
        self.titleLabel.textAlignment = NSTextAlignmentCenter;
        self.titleLabel.font = [UIFont systemFontOfSize:14];
        [self addSubview:self.titleLabel];
        [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.bottom.equalTo(self).offset(0);
            make.top.equalTo(self.iconImageView.mas_bottom);
        }];
    }
    return self;
}

@end




@implementation EnhanceItem

@end


